package com.xinyiweidai.xinyiweidai.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.bean.ContactsBean;

import java.util.ArrayList;
import java.util.List;

/**
 * classname: $CLASS_NAME$
 * Data：2018/9/28-19:51
 * Author: lanxiong
 * Description:
 */

public class ContactsListAdapter extends RecyclerView.Adapter<ContactsListAdapter.ViewHolder> {


    private List<ContactsBean> contactsList = new ArrayList<>();


    public ContactsListAdapter(List<ContactsBean> contactsList) {
        this.contactsList = contactsList;
    }


    @Override
    public ContactsListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contacts, parent,false));
    }


    @Override
    public void onBindViewHolder(ContactsListAdapter.ViewHolder holder, int position) {
        ContactsBean bean = contactsList.get(position);
        holder.checkbox_contacts.setText(bean.getName());
        holder.tv_contacts.setText(bean.getNum());

        holder.checkbox_contacts.setOnCheckedChangeListener((compoundButton, b) -> onItemCheckedListener.onChecked(position,b));
    }


    @Override
    public int getItemCount() {
        return contactsList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        private CheckBox checkbox_contacts;
        private TextView tv_contacts;

        private ViewHolder(View itemView) {
            super(itemView);
            checkbox_contacts = itemView.findViewById(R.id.checkbox_contacts);
            tv_contacts = itemView.findViewById(R.id.tv_contacts);
        }
    }


    public void setOnItemCheckedListener(OnItemCheckedListener onItemCheckedListener) {
        this.onItemCheckedListener = onItemCheckedListener;
    }

    private OnItemCheckedListener onItemCheckedListener;

    public interface OnItemCheckedListener {
        void onChecked(int position,boolean checked);
    }
}
